#include <vector>
#include <map>

using namespace std;

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        map<int,int> hash;
        stack<int> stack;
        vector<int> res;
        for(int i = nums2.size()-1; i >= 0; i--)
        {
            while(!stack.empty() && stack.top() <= nums2[i])
                stack.pop();
            if(stack.empty())
                hash.insert(pair<int,int>(nums2[i],-1));
            else
                hash.insert(pair<int,int>(nums2[i],stack.top()));
            stack.push(nums2[i]);
        }

        for(int i = 0; i < nums1.size(); i++)
        {
            res.push_back(hash[nums1[i]]);
        }

        return res;
    }
};